package com.om.example.loginservice;

public class LoginService {

	private final IAccountRepository accountRepository;
	 
	   public LoginService(IAccountRepository accountRepository) {
	      this.accountRepository = accountRepository;
	   }
	 
	   private String previousAccountId = "";
	      // snip ...
	 
	   public void login(String accountId, String password) {
	      IAccount account = accountRepository.find(accountId);
	 
	      
	      if (account == null)
	          throw new AccountNotFoundException();
	      
	      if (account.passwordMatches(password)) {
	          if (account.isLoggedIn())
	             throw new AccountLoginLimitReachedException();
	          if (account.isRevoked())
	              throw new AccountRevokedException();
	          
	         account.setLoggedIn(true);
	      } else {
	         if (previousAccountId.equals(accountId))
	            ++failedAttempts;
	         else {
	            failedAttempts = 1;
	            previousAccountId = accountId;
	         }
	      }
	 
	      if (failedAttempts == 3)
	         account.setRevoked(true);
	   }
	   
	   private int failedAttempts = 0;
	      // snip ...
	 
	   
}
